function validateForm(model, form, rules) {
    var campo, regla, mensaje, rule, valor;
    var mensaje_global = "";
    var error = false;
    
    for (key in rules) {
        campo = rules[key][0];
        regla = rules[key][1];
        
        for (key2 in regla) {
            mensaje = regla[key2][0];
            rule = regla[key2][1];
            valor = $("*[name='data[" + model + "][" + campo + "]']").val();
                            
            if (!check(model, valor, rule)) {
                mensaje_global += "<div class='form-error'><strong>»</strong> " + mensaje + ".</div>";
                error = true;
                break;
            }
        }
    }
    
    if (error) {
        $("#dialog-modal-errores").html("<div class='form-error-header'>Por favor corrija estos errores:</div>" + mensaje_global);
        $("#dialog-modal-errores").data("kendoWindow").center().open();
        return false;
    }

    return true; 
}

function check(model, value, rule) {
    if (rule == "unique" && !empty(value)) {
        var exist = false;
        $.ajax({
            type: 'GET',
            url: '/soporte/' + model + 's/ajax_exists/' + value,
            async: false,
            success: function(res) {
                if (!empty(res)) {
                    exist = true;
                }
            }
        });
        return !exist;
    }
    if (rule == "notEmpty") {
        return !empty(value);
    }
    if (rule == "numeric") {
        return !isNaN(value);
    }
    if (rule == "alphaNumeric") {
        var pattern = /^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/;
        return pattern.test(value);
    }
    if (rule == "email") {
        var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
        return pattern.test(value);
    }
    if (rule == "ip") {
        var pattern = /^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/;
        return pattern.test(value);
    }
    return false;
}